package util.crew;

import java.text.SimpleDateFormat;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import util.CentralControl;
import util.tool.Requests;
import util.tool.Sqlite3;

/**
 * CCTV爬虫
 * @author kong
 *
 */
public class CCTV extends Crew {
// https://so.cntv.cn/service/mobile_news_search.php?controller=list&action=searchList&novideo=true&n=100&wd=%E9%A3%9F%E5%93%81&sort=relevance&p=1
// http://api.cportal.cctv.com/api/rest/articleInfo?id=ArtiO2LzAXLcjYTgjGsmLBFi190311

	private String keyWord;
	@Override
	/**
	 * 开始一条线程
	 */
	public void run() {
		// TODO Auto-generated method stub
		dealData(keyWord);
	}
	@Override
	public void work(String s) {
		// TODO Auto-generated method stub
		keyWord=s;
	}
	/**
	 * 处理单条新闻
	 * @param page 第几页
	 * @param keyword 关键词
	 * @return 请求所返回的数据
	 */
	private String getPerData(int page, String keyword) {
		String url = "http://so.cntv.cn/service/mobile_news_search.php?controller=list&action=searchList&novideo=true&n=20&wd=" + keyword + "&sort=relevance&p=";
		url = url + page;
		try {
			return requests.proxyGet(url, null);
		} catch (Exception e) {
			// TODO: handle exception
			return "{\"flag\":\"WhatTheFuck\"}";
		}
	}
	/**
	 * 获取文章内容
	 * @param id 文章id
	 * @return 文章内容
	 */
	private String getContentById(String id) {
		String url = "http://api.cportal.cctv.com/api/rest/articleInfo?id=" + id;
		try {
			return requests.proxyGet(url, null); 
		} catch (Exception e) {
			// TODO: handle exception
			return "";
		}
	}
	/**
	 * 线程行为定义
	 * @param keyWord 所需关键词
	 */
	private void dealData(String keyWord) {
//		String ss = "insert into data (url,source,publisher,summitTime,content,title,tags,id)" +
//				"values('%s','%s','%s','%s','%s','%s','%s','%s');";
		int offset = 0;
		int total = 0;
		while (true) {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject;
			try {
				String data = getPerData(offset, keyWord);
				jsonObject = (JsonObject) parser.parse(data);				
				if (jsonObject.get("total").getAsInt() < total) break; // 没数据了
			} catch (Exception e) {
				// TODO: handle exception
				continue;
			}
			offset += 1; total += 20;
			JsonArray data2 = jsonObject.get("items").getAsJsonArray();

			for (int i = 0; i < data2.size(); i++) {
				if (!CentralControl.run) return;
				try {
					JsonObject tmp = (JsonObject)data2.get(i);
					String id = tmp.get("itemID").getAsString();
					String publisher = "佚名"; // 央视新闻没有提供发布者信息
					String title = tmp.get("itemTitle").getAsString();
					String imgUrl = tmp.get("itemImage").getAsJsonObject().get("imgUrl1").getAsString();
					title = removeHtmlTags(title);
					String url = tmp.get("detailUrl").getAsString();
					String summitTime = tmp.get("pubTime").getAsString();
					JsonObject jsonLocal = (JsonObject)parser.parse(getContentById(id));
//				if (!jsonLocal.get("success").getAsBoolean()) continue; // 玄学跳过
					String content = jsonLocal.get("content").getAsString();
					content = removeHtmlTags(content);
					String tags = getTag(content);
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					summitTime = String.valueOf(sdf.parse(summitTime).getTime()/1000);
					
					sql.update(String.format(insert, url, "2", publisher, summitTime, content, title, tags, id, imgUrl));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			}
		}
		System.err.println("CCTV:" + keyWord);
	}
	/**
	 * 数据更新
	 * id 是数据库中的id
	 */
	@Override
	public boolean update(String id) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonLocal = (JsonObject)parser.parse(getContentById(id));
//			if (!jsonLocal.get("success").getAsBoolean()) continue; // 玄学跳过
			String content = jsonLocal.get("content").getAsString();
			content = removeHtmlTags(content);
			
			return sql.update("update data set content='" + content + "' where id='" + id +"'");
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
}
